package cn.org.donald.controller;

import cn.org.donald.framework.constant.InfoConstant;
import cn.org.donald.framework.controller.BaseController;
import cn.org.donald.framework.pojo.dto.UploadResult;
import cn.org.donald.framework.pojo.vo.HttpResult;
import cn.org.donald.framework.pojo.dto.LoginUser;
import cn.org.donald.framework.util.FileUtil;
import cn.org.donald.framework.util.RequestUtil;
import cn.org.donald.framework.util.SendMsgUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ： Donald
 * @date ： 2020/10/17 23:33
 * @description：
 */
@RestController
@RequestMapping("/test")
@RequiresRoles("admin")
public class TestController extends BaseController {

    @GetMapping(value = "/",produces = "application/json; charset=UTF-8")
    public String hello(){
        return "hello";
    }

    @GetMapping("/getLoginUser")
    @ApiOperation(value = "测试获取登录的user主体")
    public HttpResult getLoginUser(HttpServletRequest request){
        LoginUser loginUser = RequestUtil.getLoginUser(request);
        return toResultSuccess(loginUser);
    }

    @GetMapping("/uploadFile")
    public HttpResult uploadFile(MultipartFile multipartFile){
        UploadResult uploadResult = FileUtil.upload(multipartFile, "test");
        return uploadResult.getSuccess() ? toResultSuccess(InfoConstant.UPLOAD_FILE_SUCCESS,uploadResult) : toResultFail(InfoConstant.UPLOAD_FILE_FAIL);
    }

    @GetMapping("/sendSms")
    public HttpResult sendSms(String phone){
        SendMsgUtil.sendPhoneVerify(6L,phone,600);
        return toResultSuccess();
    }
}
